import uuid
from datetime import datetime

from django.core.cache import cache
from django.db import models

from uuuu.models import BaseModel


# Create your models here.
# 导航
class Gps(BaseModel):
    name = models.CharField(max_length=100,verbose_name='名称')
    display_name = models.CharField(max_length=100,verbose_name='显示名称')
    weight = models.IntegerField(default=0,verbose_name='权重')
    url = models.URLField(blank=True,verbose_name='连接')
    wailian = models.URLField(blank=True)
    class Meta:
        db_table = 'tb_gps'
        verbose_name = '导航'
        verbose_name_plural = verbose_name
        ordering = ['order']




class Course(BaseModel):
    name = models.CharField(max_length=100,verbose_name='名称')
    level = models.CharField(max_length=100,verbose_name='等级')
    dep = models.CharField(max_length=100,verbose_name='描述')
    parent = models.ForeignKey('self',on_delete=models.CASCADE,blank=True,null=True,verbose_name='父级')
    class Meta:
        db_table = 'tb_course'
        verbose_name = '课程'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class Img(BaseModel):
    name = models.CharField(max_length=100,verbose_name='名称')
    img = models.CharField(max_length=300,verbose_name='图片')
    link = models.URLField(blank=True,verbose_name='链接')
    note = models.CharField(max_length=100,verbose_name='描述')
    class Meta:
        db_table = 'tb_img'
        verbose_name = '图片'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        cache_key = 'day01:home:img:list'
        cache.delete(cache_key)
    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)
        cache_key = 'day01:home:img:list'
        cache.delete(cache_key)

# 图片上传路径定义方法
def huoqutupian(instance, filename):
    houzhui =filename.split('.')[-1]
    print(houzhui)
    filename = f"{uuid.uuid4()}.{houzhui}"
    now = datetime.now()
    return f"tupian/{now.year}/{now.month}/{now.day}/{filename}"

# 图片模型
class Tupian(models.Model):
    name = models.CharField(max_length=100,verbose_name='名称')
    img = models.ImageField(upload_to=huoqutupian,verbose_name='图片',blank=True,null=True)
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    class Meta:
        db_table = 'tb_tupian'
        verbose_name = '图片'
        verbose_name_plural = verbose_name



















